﻿#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include <iostream>

#include "SDL.h"
#include "SDL_image.h"
#undef main

using namespace std;

int main()
{
    //创建窗口的尺寸
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;

    //运行主窗口
    SDL_Window* screen = nullptr;

    //主窗口绘图
    SDL_Surface* gScreenSurface =nullptr;

    //The image we will load and show on the screen
    SDL_Surface* background= nullptr;

    //SDL事件对象
    SDL_Event event;
    bool quit = false;

    //首先初始化初始化SDL视频子系统
    if(SDL_Init(SDL_INIT_EVERYTHING)<0)
    {
        std::cout << SDL_GetError() << std::endl;
        return -1;
    }
    //创建窗口
    screen=SDL_CreateWindow("SHOW BMP",//窗口标题
                            SDL_WINDOWPOS_UNDEFINED,//窗口位置设置
                            SDL_WINDOWPOS_UNDEFINED,
                            SCREEN_WIDTH,//窗口的宽度
                            SCREEN_HEIGHT,//窗口的高度
                            SDL_WINDOW_SHOWN//显示窗口
                            );
    if(screen==nullptr)
    {
        std::cout << SDL_GetError() << std::endl;
        return -1;
    }

    //获取窗口对应的surface
    gScreenSurface=SDL_GetWindowSurface(screen);
    SDL_FillRect(gScreenSurface, nullptr, 0xff009900);

    //加载图片
    background= IMG_Load("E://workspace//FFMEPG//SDL//sources//PNG3.png");

    if( background== nullptr)
    {
        std::cout << SDL_GetError() << std::endl;
        return -1;
    }

    SDL_BlitSurface(background, nullptr, gScreenSurface, nullptr);
    SDL_UpdateWindowSurface(screen);//更新显示copy the window surface to the screen

    while (quit == false) //当用户不想退出时
    {
        //函数SDL_PollEvent() 的作用是从事件队列底部取出一个事件并将其事件数据粘贴到一个SDL_Event类型的结构体中
        while (SDL_PollEvent(&event))
        {
            //若用户点击量窗口右上角的关闭按钮
            if(event.type == SDL_QUIT)
            {
                //退出程序
                quit = true;
            }
        }
    }

    //释放内存
    SDL_FreeSurface(gScreenSurface);
    gScreenSurface = nullptr;

    SDL_FreeSurface( background);//释放空间
    background= nullptr;

    SDL_DestroyWindow(screen);//销毁窗口
    screen = nullptr;

    SDL_Quit();//退出SDL

    return 0;
}

